package com.jiantong.web.open.app.user;

import com.google.common.collect.Maps;
import com.jiantong.user.model.BreedFactoryStable;
import com.jiantong.user.service.BreedFactoryStableReadService;
import com.jiantong.user.service.BreedFactoryStableWriteService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

/**
 * Created by mz on 24/7/17.
 *
 */
@OpenBean
@Slf4j
public class BreedFactoryStableApis {

    @RpcConsumer
    private BreedFactoryStableWriteService breedFactoryStableWriteService;

    @RpcConsumer
    private BreedFactoryStableReadService breedFactoryStableReadService;


    @OpenMethod(
            key = "add.a.stable.for.breed.factory",
            paramNames = {"breedFactoryStable"},
            httpMethods = RequestMethod.POST
    )
    public Long addAStableForBreedFactory(BreedFactoryStable breedFactoryStable){
        if(breedFactoryStable == null){
            log.error("breedFactoryStable should be not null");
            throw new OPClientException(ExceptionCode.OBJECT_NOT_EXIST.value(),
                    "breedFactoryStable should be not null");
        }

        Response<Long> response = breedFactoryStableWriteService.create(breedFactoryStable);

        if(!response.isSuccess()){
            log.error("failed to add a stable for breed factory , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),
                    response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "update.a.stable.of.breed.factory",
            paramNames = {"breedFactoryStable"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean updateAStableOfBreedFactory(BreedFactoryStable breedFactoryStable){
        if(breedFactoryStable.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<Boolean> response = breedFactoryStableWriteService.update(breedFactoryStable);

        if(!response.isSuccess()){
            log.error("failed to update a stable of breed factory ,case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_WRITE_FAIL.value(),
                    response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "delete.stable.of.breed.factory.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.DELETE
    )
    public Boolean delete(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<Boolean> response = breedFactoryStableWriteService.delete(id);

        if(!response.isSuccess()){
            log.error("failed to delete a stable of breed factory , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_DELETE_FAIL.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "find.a.stable.of.breed.factory.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public BreedFactoryStable findById(Long id){
        if(id == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }

        Response<BreedFactoryStable> response = breedFactoryStableReadService.findById(id);

        if(!response.isSuccess()){
            log.error("failed to find stable of breed factory , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "paging.stable.by.breed.factory.id",
            paramNames = {"pageNo","pageSize","breedFactoryId","status"},
            httpMethods = RequestMethod.GET
    )
    public Paging<BreedFactoryStable> pagingBreedFactoryStable(Integer pageNo,Integer pageSize,
                                                               Long breedFactoryId, Integer status){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId should be not null");
        }
        Map<String ,Object> criteria = Maps.newHashMap();

        criteria.put("breedFactoryId" , breedFactoryId);
        if(status != null){
            criteria.put("status",status);
        }

        Response<Paging<BreedFactoryStable>> response = breedFactoryStableReadService.paging(pageNo,pageSize,criteria);

        if(!response.isSuccess()){
            log.error("failed to paging stable of breed factory , case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }

    @OpenMethod(
            key = "find.stable.list.by.breed.factory.id",
            paramNames = {"breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public List<BreedFactoryStable> findStableList(Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "breedFactoryId should be not null");
        }

        Response<List<BreedFactoryStable>> response = breedFactoryStableReadService.findStableList(breedFactoryId);

        if(!response.isSuccess()){
            log.error("failed to find stable list , case = {}", response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }
        return response.getResult();
    }

    @OpenMethod(
            key = "breed.stable.is.exist",
            paramNames = {"breedFactoryId"},
            httpMethods = RequestMethod.GET
    )
    public Long isExist(Long breedFactoryId){
        if(breedFactoryId == null){
            log.error("breedFactoryId should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),"breedFactoryId should be not null");
        }
        Response<Long> response = breedFactoryStableReadService.isExist(breedFactoryId);
        if(!response.isSuccess()){
            log.error("failed to count stables case = {}",response.getError());
            throw new OPServerException(ExceptionCode.DATA_NOT_FOUND.value(),response.getError());
        }

        return response.getResult();
    }
}
